package com.zl.rbac.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * Druid数据源配置类
 * @ClassName: DruidConfig
 * @author: suke
 * @since: 2021/1/5 14:49
 */
@Configuration
public class DruidConfig {
     @Value("${spring.datasource.driver-class-name}")
     private String driverClass;
    @Value("${spring.datasource.url}")
     private String jdbcUrl;
    @Value("${spring.datasource.username}")
     private String username;
    @Value("${spring.datasource.password}")
     private String password;
    @Value("${druid.maxsize}")
     private int  maxSize;
    @Value("${druid.minsize}")
     private int minSize;
    @Value("${druid.initsize}")
    private int initSize;


    @Bean(name="dataSource")
    public DataSource createDataSource(){
        DruidDataSource ds = new DruidDataSource();
        //设置数据库四大参数
        ds.setDriverClassName(driverClass);
        ds.setUrl(jdbcUrl);
        ds.setUsername(username);
        ds.setPassword(password);

        //连接池的参数
        ds.setMaxActive(maxSize);
        ds.setMinIdle(minSize);
        ds.setInitialSize(initSize);

        return ds;
    }
}
